﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using M3G;

/**
 * (注意) M3Gの仕様ではGetIndices()はトライアングルストリップの時トライアングルの集合として返ってくる。
 * GetIndexCount()も同じ。入力したindicesがそのまま帰ってくるわけでないことに注意する。ラインも同じ。
 * */

namespace MyTestProject {

    public class TestIndexBuffer {
        [Fact]
        public void TestLines1 () {
            var type           = PrimitiveType.Lines;
            var primitiveCount = 3;
            var firstIndex     = 10;
            var ibuf = new IndexBuffer (type, primitiveCount, firstIndex);


            Assert.Equal (type, ibuf.PrimitiveType);
            Assert.Equal (6, ibuf.IndexCount);

            var expected   = new int[] { 10, 11, 12, 13, 14, 15 };

            Assert.Equal (expected, ibuf.GetIndices());
        }

        [Fact]
        public void TestLines2 () {
            var type           = PrimitiveType.Lines;
            var primitiveCount = 3;
            var indices        = new int[] { 10, 11, 12, 13, 14, 15 };
            var ibuf           = new IndexBuffer (type, primitiveCount, indices);

            Assert.Equal (type, ibuf.PrimitiveType);
            Assert.Equal (6, ibuf.IndexCount);

            var expected   = new int[] { 10, 11, 12, 13, 14, 15 };

            Assert.Equal (expected, ibuf.GetIndices());
        }

        [Fact]
        public void TestLineStrips1 () {
            var type         = PrimitiveType.Lines;
            var stripLengths = new int[] { 3, 2 };
            var firstIndex   = 10;
            var ibuf         = new IndexBuffer (type, stripLengths, firstIndex);

            Assert.Equal (type, ibuf.PrimitiveType);
            Assert.Equal (6, ibuf.IndexCount);

            // LineStripsはLinesに分解されて返ってくるのがM3Gの仕様
            var expected   = new int[] { 10, 11, 11, 12, 13, 14 };

            Assert.Equal (expected, ibuf.GetIndices());
        }

        [Fact]
        public void TestLineStrips2 () {
            var type         = PrimitiveType.Lines;
            var stripLengths = new int[] { 3, 2 };
            var indices      = new int[] { 10, 11, 12, 13, 14 };
            var ibuf         = new IndexBuffer (type, stripLengths, indices);

            Assert.Equal (type, ibuf.PrimitiveType);
            Assert.Equal (6, ibuf.IndexCount);

            // LineStripsはLinesに分解されて返ってくるのがM3Gの仕様
            var expected   = new int[] { 10, 11, 11, 12, 13, 14 };

            Assert.Equal (expected, ibuf.GetIndices());
        }

        [Fact]
        public void TestTriangles1 () {
            var type           = PrimitiveType.Triangles;
            var primitiveCount = 2;
            var firstIndex     = 10;
            var ibuf           = new IndexBuffer (type, primitiveCount, firstIndex);

            Assert.Equal (type, ibuf.PrimitiveType);
            Assert.Equal (6, ibuf.IndexCount);

            var expected = new int[] { 10, 11, 12, 13, 14, 15 };

            Assert.Equal (expected, ibuf.GetIndices());
        }

        [Fact]
        public void TestTriangles2 () {
            var type           = PrimitiveType.Triangles;
            var primitiveCount = 2;
            var indices        = new int[] { 10, 11, 12, 13, 14, 15 };
            var ibuf           = new IndexBuffer (type, primitiveCount, indices);


            Assert.Equal (type, ibuf.PrimitiveType);
            Assert.Equal (6, ibuf.IndexCount);

            var expected   = new int[] { 10, 11, 12, 13, 14, 15 };

            Assert.Equal (expected, ibuf.GetIndices());
        }

        [Fact]
        public void TestTriangleStrips1 () {
            var type         = PrimitiveType.Triangles;
            var stripLengths = new int[] { 4, 4 };
            var firstIndex   = 10;
            var ibuf         = new IndexBuffer (type, stripLengths, firstIndex);

            Assert.Equal (type, ibuf.PrimitiveType);
            Assert.Equal (12, ibuf.IndexCount);

            // TriangleStripsはTrianglesに分解されて返ってくるのがM3Gの仕様
            var expected   = new int[] { 10, 11, 12, 11, 12, 13, 14, 15, 16, 15, 16, 17 };

            Assert.Equal (expected, ibuf.GetIndices());
        }

        [Fact]
        public void TestTriangleStrips2 () {
            var type         = PrimitiveType.Triangles;
            var stripLengths = new int[] { 4, 4 };
            var indices      = new int[] { 10, 11, 12, 13, 14, 15, 16, 17 };
            var ibuf         = new IndexBuffer (type, stripLengths, indices);


            Assert.Equal (type, ibuf.PrimitiveType);
            Assert.Equal (12, ibuf.IndexCount);

            // TriangleStripsはTrianglesに分解されて返ってくるのがM3Gの仕様
            var expected   = new int[] { 10, 11, 12, 11, 12, 13, 14, 15, 16, 15, 16, 17 };

            Assert.Equal (expected, ibuf.GetIndices ());
        }

        [Fact]
        public void TestPointSprites1 () {
            var type          = PrimitiveType.PointSprites;
            var primtiveCount = 4;
            var firstIndex    = 10;
            var ibuf          = new IndexBuffer (type, primtiveCount, firstIndex);


            Assert.Equal (type, ibuf.PrimitiveType);
            Assert.Equal (4, ibuf.IndexCount);

            var expected   = new int[] { 10, 11, 12, 13 };

            Assert.Equal (expected, ibuf.GetIndices ());
        }

        [Fact]
        public void TestPointSprites2 () {
            var type          = PrimitiveType.PointSprites;
            var primtiveCount = 4;
            var indices         = new int[] { 10, 11, 12, 13 };
            var ibuf            = new IndexBuffer (type, primtiveCount, indices);

            Assert.Equal (type, ibuf.PrimitiveType);
            Assert.Equal (4, ibuf.IndexCount);

            var expected   = new int[] { 10, 11, 12, 13 };

            Assert.Equal (expected, ibuf.GetIndices ());
        }

        [Fact]
        public void TestDupliate () {
            var type           = PrimitiveType.Lines;
            var primitiveCount = 3;
            var firstIndex     = 10;
            var srcIndices     = new int[6];
            var dupIndices     = new int[6];
            
            var ibuf = new IndexBuffer (type, primitiveCount, firstIndex);
            ibuf.UserID = 100;

            var dup = (IndexBuffer)ibuf.Duplicate ();
    
            Assert.Equal (ibuf.PrimitiveType, dup.PrimitiveType);
            Assert.Equal (ibuf.IndexCount, dup.IndexCount);
            Assert.Equal (ibuf.GetIndices (), dup.GetIndices ());
            Assert.Equal (ibuf.UserID, dup.UserID);
        }
    }
}
